package com.gf.pf.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.gf.pf.model.Role;
import com.gf.pf.model.RoleResource;
import com.gf.pf.model.Ztree;

/**
 * @describe 角色
 * @date     2015年3月7日
 */
public interface RoleMapper {

	/**
	 * 根据用户ID查询角色SQL
	 */
	final String GET_BY_USERID = "SELECT * FROM sys_role WHERE role_isvalid = 1 and role_id IN (SELECT role_id FROM sys_user_role WHERE user_id = #{userId})";
	
	/**
	 * 保存角色SQL
	 */
	final String SAVE_ROLE = "INSERT INTO sys_role VALUES(#{id}, #{roleName}, #{roleMemo}, ${roleIsvalid})";
	
	/**
	 * 根据角色ID查询角色信息SQL
	 */
	final String GET_ROLE_BY_ID = "SELECT role_id as id, role_name, role_memo, role_isvalid FROM sys_role WHERE role_id = #{roleId}";
	
	/**
	 * 修改角色SQL
	 */
	final String MODIFY_ROLE_BY_ID = "UPDATE sys_role SET role_name = #{roleName}, role_memo = #{roleMemo}, role_isvalid = ${roleIsvalid} WHERE role_id = #{id}";
	
	/**
	 * 删除角色SQL
	 */
	final String DELETE_ROLE_BY_ID = "DELETE FROM sys_role WHERE role_id = #{roleId}";
	
	/**
	 * 根据角色ID查询所包含的资源SQL
	 */
	final String ROLE_RESOURCE_ZTREE = "SELECT r.resource_id as id, r.resource_parent_id as pId, r.resource_name as name FROM sys_resource r LEFT JOIN sys_role_resource rr on r.resource_id = rr.resource_id WHERE rr.role_id = #{roleId} order by rr.resource_id";
	
	/**
	 * 删除角色所包含的资源SQL
	 */
	final String DELETE_ROLE_RESOURCE = "DELETE FROM sys_role_resource WHERE role_id = #{roleId}";
	
	/**
	 * 保存角色所包含的资源SQL
	 */
	final String SAVE_ROLE_RESOURCE = "INSERT INTO sys_role_resource VALUES(#{roleId}, #{resourceId})";
	
	/**
	 * 
	 * @describe 根据用户ID查询角色
	 * @param userId
	 * @return
	 */
	@Select(GET_BY_USERID)
	List<Role> getRoles(String userId);
	
	/**
	 * 
	 * @description 保存角色
	 * @return Integer
	 */
	@Insert(SAVE_ROLE)
	Integer saveRole(Role role);
	
	/**
	 * 
	 * @description 根据角色ID查询角色信息
	 * @return Role
	 */
	@Select(GET_ROLE_BY_ID)
	Role getRole(String roleId);
	
	/**
	 * 
	 * @description 修改角色
	 * @return Integer
	 */
	@Update(MODIFY_ROLE_BY_ID)
	Integer modifyRole(Role role);
	
	/**
	 * 
	 * @description 删除角色
	 * @return Integer
	 */
	@Delete(DELETE_ROLE_BY_ID)
	Integer deleteRole(String roleId);
	
	/**
	 * 
	 * @description 根据角色ID查询所包含的资源
	 * @return List<Ztree>
	 */
	@Select(ROLE_RESOURCE_ZTREE)
	List<Ztree> getResourceByRole(String roleId);
	
	/**
	 * 
	 * @description 删除角色所包含的资源
	 * @return Integer
	 */
	@Delete(DELETE_ROLE_RESOURCE)
	Integer deleteRoleResource(String roleId);
	
	/**
	 * 
	 * @description 保存角色所包含的资源
	 * @return Integer
	 */
	@Insert(SAVE_ROLE_RESOURCE)
	Integer saveRoleResource(RoleResource rr);
}
